import { qqmapKey } from '../../model/api';
import {timeFormat} from '../../model/utils/common';

export default {
    namespace: 'station',
    state: {
        price: {},
        activeMarker: 0,
        location: {},
        markers: [],
        stationList : []
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *init({ payload }, { put, call, select }){
            const {latitude, longitude, keyword} = payload;
            console.log('地理位置latitude:', latitude);
            console.log('地理位置longitude:', longitude);
            const stationList = yield put.resolve({
                type : '$request/get',
                payload : {
                    url : '/qqmap/ws/place/v1/search',
                    data: {
                        key: qqmapKey,
                        keyword: keyword|| '加油站',
                        boundary: `nearby(${latitude},${longitude},1000)&page_size=20`
                    }
                }
            });
            const markers = stationList.map((item,index) => {
                return {
                    iconPath: 'https://codermoyv.gitee.io/coder-moyv/assets/images/wechat/e_life_helper/icon_oli.png',
                    id: index,
                    markerId: item.id,
                    title: item.title,
                    address: item.address,
                    latitude: item.location.lat,
                    longitude: item.location.lng,
                    width: 32,
                    height: 32
                };
            });
            const active = markers[0];
            if(active){
                active.iconPath ='https://codermoyv.gitee.io/coder-moyv/assets/images/wechat/e_life_helper/icon_oli_active.png';//将第一位设置为选中
                // 计算价格
                yield put({
                    type: 'getOilPrice',
                    payload: active
                });
            }
            yield put({
                type: 'save',
                payload: {
                    stationList,
                    markers,
                    location: {
                        latitude, longitude
                    }
                }
            });

            
        },
        /**
         * 获取地区价格
         * @param {*} param0 
         * @param {*} param1 
         */
        *getOilPrice({ payload }, { put, call, select }) {
            const address = payload.address||'';
            const province = address.split('省')[0];
            const price = yield put.resolve({
                type: '$request/post',
                payload: {
                    url : '/api/getOilPrice',
                    data: {
                        'province': province,
                        'date': timeFormat(new Date, 'YYYY-MM-DD')
                    }
                }
            });
            yield put({
                type : 'save',
                payload : {
                    price  : price||{}
                }
            });
        },
    }
};